<template>
  <el-container>
<!--    <el-header>-->
<!--      -->
<!--    </el-header>-->
    <el-main style="width: 100%">
      <el-card class="box-card" style="width: 98%;">
        <template #header>
          <div class="card-header">
            <el-row>
              <el-col :span="20">
                <el-tag style="margin-left: 20%" size="large">{{userInfo.username}}的主页</el-tag>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="doFollow">
                  <a v-if="followStatus === '1'">已关注</a>
                  <a v-else> 关注 </a>
                </el-button>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="doSession">聊天</el-button>
              </el-col>
            </el-row>
          </div>
        </template>
        <div>
          <el-row>
            <el-col>
              <el-tag size="large">个人简介</el-tag>
            </el-col>
          </el-row>
          {{userInfo.description}}
        </div>
      </el-card>

    </el-main>
  </el-container>
</template>

<script>
import request from "@/utils/request";
import {getUserInfo} from "@/utils/owninfo";
import router from "@/router";

export default {
  name: "OwnPage",
  data() {
    return {
      userInfo:'',
      followStatus:null,
    }
  },
  created() {
    this.init()
    if(getUserInfo()!==undefined || getUserInfo() !== null){
      this.loadFollowStatus()
    }
  },
  mounted() {
  },
  methods:{
    init(){
      this.userInfo = JSON.parse(this.$route.query.user);
    },
    loadFollowStatus(){
      request({
        url: '/follow/getfollowbyuseridandtouserid',
        method: 'get',
        params:{
          userid: getUserInfo(),
          touserid: this.userInfo.userid,
        }
      }).then(response => {
        console.log(response)
        this.followStatus = response.data.followStatus
      }).catch(error => {
        console.info(error)
        this.$message.error("网络错误");
      })
    },
    doFollow(){
      request({
        url: '/follow/insertfollow',
        method: 'get',
        params:{
          userid: getUserInfo(),
          touserid: this.userInfo.userid
        }
      }).then(response => {
        console.log(response)
        this.followStatus = response.data.follow.status
      }).catch(error => {
        console.info(error)
        this.$message.error("网络错误");
      })
    },
    doSession(){
      request({
        url: '/session/insertsession',
        method: 'get',
        params:{
          userid: getUserInfo(),
          touserid: this.userInfo.userid,
          sessiontype: "private",
        }
      }).then(response => {
        console.log(response)
        var session = response.data.session
        router.push({
          name:"Message",//这个name就是你刚刚配置在router里边的name
          params:{}
        })
      }).catch(error => {
        console.info(error)
        this.$message.error("网络错误");
      })
    },
  }
}
</script>

<style scoped>

</style>
